﻿using System;
using System.Collections.Generic;
using System.Linq;
using T.ServiceComponent.Cache.Service;

namespace T.FAS.Runtime.Base.Common
{
    public class FASCacheService
    {
        private static readonly object syncObj = new object();
        private static CacheService cacheService;
        private static FASCacheService service;
        public static FASCacheService GetInstance()
        {
            if (service == null)
            {
                lock (syncObj)
                {
                    if (service == null)
                        service = new FASCacheService();
                }
            }
            return service;
        }
        private FASCacheService()
        {
            cacheService = CacheService.GetInstance("DefaultPool");
        }
        /// <summary>
        /// GetValueFromHash
        /// </summary>
        /// <returns></returns>
        public T GetValueFromHash<T>(string hashId, string key)
        {
            T value;
            using (ICacheClient client = cacheService.GetClient())
            {
                value = client.GetValueFromHash<T>(hashId, key);
            }
            return value;
        }
        /// <summary>
        /// SetEntryInHash
        /// </summary>
        /// <returns></returns>
        public bool SetEntryInHash<T>(string hashId, string key, T value)
        {
            using (ICacheClient client = cacheService.GetClient())
            {
                return client.SetEntryInHash(hashId, key, value);
            }
        }
        /// <summary>
        /// RemoveEntryFromHash
        /// </summary>
        /// <returns></returns>
        public bool RemoveEntryFromHash(string hashId, string key)
        {
            using (ICacheClient client = cacheService.GetClient())
            {
                return client.RemoveEntryFromHash(hashId, key);
            }
        }
    }
}
